<template>
    <div>
        <headers />
        <div class="container">
            <div>
                <div class="userCenterWarp">
                    <div class="userCenterTitle">个人中心</div>
                    <el-row :gutter="30">
                        <el-col :span="4">
                            <sideNav />
                        </el-col>
                        <el-col :span="20" class="mt-30">
                            <el-breadcrumb separator-class="el-icon-arrow-right" class="user-breadcrumb">
                                <el-breadcrumb-item><strong>我的发布（{{ total }}条）</strong></el-breadcrumb-item>
                            </el-breadcrumb>

                            <el-table :data="list" border style="width: 100%" v-loading="loading">
                                <el-table-column prop="id" label="ID" width="50" align="center"></el-table-column>
                                <el-table-column prop="pic" label="封面图片" width="100" align="center">
                                    <template slot-scope="scope">
                                        <el-image :src="scope.row.image" style="width: 60px; height: 60px;"
                                            lazy></el-image>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="title" label="标题" show-overflow-tooltip>
                                </el-table-column>
                                <el-table-column prop="view_num" label="浏览次数" width="100" align="center">
                                </el-table-column>

                                <el-table-column prop="type" label="类型" width="180" align="center">
                                    <template slot-scope="scope">
                                        {{ scope.row.type === 1 ? '视频' : '资料' }}
                                    </template>
                                </el-table-column>

                                <!-- <el-table-column prop="status" label="发布状态" width="100" align="center">
                                    <template slot-scope="scope">
                                        <el-link :underline="false" type="success"
                                            v-if="scope.row.status === 1">已通过</el-link>
                                        <el-link :underline="false" type="info"
                                            v-if="scope.row.status === 0">待审核</el-link>
                                        <el-link :underline="false" type="danger"
                                            v-if="scope.row.status === 2">已删除</el-link>
                                    </template>
                                </el-table-column> -->

                                <el-table-column prop="create_time" label="发布时间" width="180" align="center">
                                </el-table-column>

                                <el-table-column fixed="right" label="操作" width="100" align="center">
                                    <template slot-scope="scope">
                                        <el-link type="danger" :underline="false"
                                            @click="handleDelete(scope.row)">删除</el-link>
                                    </template>
                                </el-table-column>
                            </el-table>
                            <div class="text-center mt-30">
                                <el-pagination @current-change="handleCurrentChange" :current-page="params.page"
                                    :page-size="params.pagesize" :page-sizes="[10, 20, 30, 40]"
                                    layout="total, prev, pager, next" :total="total" hide-on-single-page>
                                </el-pagination>
                            </div>
                        </el-col>
                    </el-row>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import headers from '@/components/header.vue'
import sideNav from './components/sideNav.vue'
import { getMyPublish, delPublish } from '@/api/apis'
export default {
    name: 'myProfile',
    components: {
        headers,
        sideNav
    },
    data () {
        return {
            loading: true,
            list: [],
            isEmpty: false,
            total: 0, // 总条数
            params: {
                page: 1, // 当前页
                pagesize: 10, // 每页条数
                lastPage: 1, // 最后一页
            }
        }
    },
    created () {
        this.getList();
    },
    methods: {
        handleCurrentChange (val) {
            this.params.page = val
            this.getList()
        },
        getList() {
            let that = this;
            that.loading = true;
            getMyPublish(this.params).then(res => {
                if (res.code === 1) {
                    that.list = res.data.data
                    that.total = res.data.total
                    //that.params.pagesize = Number(res.data.per_page)
                    that.params.lastPage = res.data.last_page
                    that.isEmpty = !that.list.length;
                } else {
                    that.$message.error(res.msg)
                }
                that.loading = false;
            })
        },
        handleDelete(row) {
            let that = this;
            this.$confirm('确认删除该条信息吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                dangerouslyUseHTMLString: true,
                customClass: 'custom-confirm',
                confirmButtonClass: 'confirmButton',
                center: true
            }).then(() => {
                delPublish({
                    id: row.id
                }).then(res => {
                    if (res.code === 1) {
                        that.$message.success(res.msg)
                        that.list.splice(that.list.indexOf(row), 1);
                    } else {
                        that.$message.error(res.msg)
                    }
                })
            })
        }
    },
}
</script>

<style scoped lang="less">
.header {
    font-size: 18px;
    color: #333;
    font-weight: 600;
    margin-bottom: 30px;
}

/deep/.el-table__header {
    th.el-table__cell {
        background-color: #FBFBFB;
    }

}
</style>